/**************************************************************************/ /**
* @file     Led_app.h
* @brief
* @version  V5.0.4
* @date     10. January 2023
******************************************************************************/

#ifndef _LED_APP_H_
#define _LED_APP_H_

#include <stdio.h>
#include "fm33le0xxa_fl_gpio.h"
#include "fm33le0xxa_fl_def.h"
#include "SensorGroup.h"
#include "SysInit.h"

#define LIGHT_ON_HIGH_VOLT  (30000U)
#define LIGHT_OFF_HIGH_VOLT (44000U)

#define LIGHT_ON_LOW_VOLT   (5000U)
#define LIGHT_OFF_LOW_VOLT  (4000U)

#define STOP_ON            (1100U)
typedef enum
{
    SWT_OFF = 0,
    SWT_ON
} SwitchStatusEnum;

typedef enum
{
    TAIL1_CHANNEL = 0,
    TAIL2_CHANNEL,
    TAIL3_CHANNEL,
    MAX_CHANNEL
} SwitchStatusTailChannelEnum;

/*定义每个位置灯PWM通道地址*/
typedef struct {
    uint32_t *channels[MAX_CHANNEL];
} ChannelAddresses;


typedef enum
{
	ANIMATION_WELCOME = 0,/**/
	ANIMATION_GOODBYE,
    ANIMATION_TRUNK_OPEN,
    ANIMATION_TRUNK_CLOSE,
    ANIMATION_INVALID,
	ANIMATION_MAXNUM,
}Animation_Em;
	

typedef enum
{
    INVALID_SIGNAL = 0, /**/
    NO_SIGNAL,
    WELCOME_SIGNAL,
    GOODBYE_SIGNAL,
    TRUNK_OPEN_SIGNAL,
    TRUNK_CLOSE_SIGNAL,
}PwmType_Em;

typedef struct
{
  PwmType_Em OldState;
  PwmType_Em NewState;
}PWM_State_St;

typedef struct
{
    PwmType_Em    type;
    uint32_t      duty;
    uint32_t      freq; 
    uint32_t      timeIn[ANIMATION_MAXNUM];
    uint32_t      timeout;
}PwmInfo_St;

extern volatile PwmInfo_St PwmInfo;

//**Animation state**
typedef enum
{
	ANIMATION_INIT = 0,/*power on wait TIME for pwm in some time*/
	ANIMATION_PREPARE, /*Prepare:Clear all light state and parameter*/
	ANIMATION_ONGOING, /*Animation is  going on*/
	ANIMATION_IDLE,    /*normal signal wait for Animation*/
}AnimationSts_Em;

typedef struct
{
    uint16_t   WaitTime;
    uint8_t    FirstFlag;
    uint8_t    WelcomeFlag;
    uint8_t    GoodbyeFlag;
    uint16_t   WelcomeStep;
    uint16_t   GoodbyeStep;
    uint32_t   WelcomeCounter;
    uint32_t   GoodbyeCounter;
    AnimationSts_Em   Status;
}AnimationInfo_St;

extern AnimationInfo_St AnimationInfo;
extern uint8_t Left_or_Right;

/***PWM OUT***/



/*** TAIL ***/
#define TAIL_PORT       GPIOB
#define TAIL1_PIN       FL_GPIO_PIN_10
#define TAIL1_CHID      FL_GPTIM_CHANNEL_1

#define TAIL2_PIN       FL_GPIO_PIN_11
#define TAIL2_CHID      FL_GPTIM_CHANNEL_2

#define TAIL3_PORT      GPIOC
#define TAIL3_PIN       FL_GPIO_PIN_1
#define TAIL3_CHID      FL_GPTIM_CHANNEL_2


/***GPIO OUT***/
/*** STOP ***/
#define STOP_PORT       GPIOC
#define STOP1_PIN       FL_GPIO_PIN_9
#define STOP2_PIN       FL_GPIO_PIN_10

#define BM_PORT         GPIOC
#define BM_PIN          FL_GPIO_PIN_0




#define Set_Stop_ON()                           \
    FL_GPIO_SetOutputPin(STOP_PORT, STOP1_PIN); \
    FL_GPIO_SetOutputPin(STOP_PORT, STOP2_PIN); 

      
#define Set_Stop_OFF()                            \
    FL_GPIO_ResetOutputPin(STOP_PORT, STOP1_PIN); \
    FL_GPIO_ResetOutputPin(STOP_PORT, STOP2_PIN); 


/***AD***/
#define STOP_SIG_PORT   GPIOD
#define STOP_SIG_PIN    FL_GPIO_PIN_2

#define TAIL_SIG_PORT   GPIOD
#define TAIL_SIG_PIN    FL_GPIO_PIN_0


/***Capture input***/
#define WELCOME_SIG_PORT        GPIOA
#define WELCOME_SIG_PIN         FL_GPIO_PIN_8
#define WELCOME_SIG_MUX         0
#define WELCOME_SIG_TIMERID     LPTIM32
#define WELCOME_SIG_CHNID       FL_LPTIM32_CHANNEL_1


SwitchStatusEnum GET_TAIL_ON_OFF_SIGN(void);
SwitchStatusEnum GET_STOP_ON_OFF_SIGN(void);

void Led_App_Animation_Det(uint32_t freq, uint32_t duty);

void Led_App_Animation_Stop_Det(void);

void Led_App_Init(void);

void Led_App_Main(void);

void Animation_Main(void);

void Led_App_CloseAllLight(void);

void SetTailAllON(void);

void Set_BM_ON(void);

void Set_BM_OFF(void);

void Change_TrunkAnimatio_State(void);
#endif
/****************************************************************************************************************************
 * End of file
 **********************************************************************************************************************/
